package code64;

import icg.EQLabel;
import icg.Quadruple;

import java.util.Set;
import java.util.Vector;

import ast.Node;

public class FCG {
	private Vector<BasicBlock> listBlock;

	public FCG() {
		listBlock = new Vector<BasicBlock>();
		buildBlocks();
	}
	
	private void newBlock(BasicBlock bb){
		if (bb.size()!=0) {
			listBlock.add(bb);
			bb.setTable();
			bb.buildTable();
		}
	}

	private void buildBlocks() {
		BasicBlock bb = new BasicBlock();;
		Set<Integer> keys = Node.icg.quadrupleMap.keySet();
		for (Integer key : keys) {
			Vector<Quadruple> list = Node.icg.quadrupleMap.get(key);
			bb = new BasicBlock();
			for (Quadruple q : list) {
				if (q.getLabel() != null) {
					newBlock(bb);
					bb = new BasicBlock();	
				} 
				bb.addQuadruple(q);
				if (q.getRes() instanceof EQLabel) {
					newBlock(bb);
					bb = new BasicBlock();	
				}
			}
		}
	}

	public String printListBlock(){
		StringBuffer sb = new StringBuffer();
		
		for(int i=0;i<listBlock.size();i++) {
			sb.append("----- b" + i + " -----\n");
			sb.append(listBlock.elementAt(i).toString() + "\n");
		}
		return sb.toString();
	}
}
